<?php

namespace App\Http\Controllers\admin;

use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\common\PublicController;
//模型
use App\Models\Area;
use App\Models\Building;
use App\Models\Room;
use App\Models\Roommember;

class RoomController extends PublicController
{
    //房间列表页面
    public function getIndex(Request $request)
    {
        /*搜索条件始*/
        //搜索条件
        $get['area'] = $request -> input('area','');
        if($get['area']){
            $this -> where .= ' -> where("area.name","like","%".$get["area"]."%")';
        }
        $get['building'] = $request -> input('building','');
        if($get['building']){
            $this -> where .= ' -> where("building.name","like","%".$get["building"]."%")';
        }
        $get['room'] = $request -> input('room','');
        if($get['room']){
            $this -> where .= ' -> where("room.name","like","%".$get["room"]."%")';
        }
        /*搜索条件末*/
        /*代理商搜索始*/
        //获取自身及旗下所有代理商
        $agentInfo = $this -> agentInfo($request -> cookie('logon')['agent_id']);
        /*代理商搜索末*/
        //获取房间信息
        $roomInfo = $this -> rangeInfo('room',$get);
        $data = $this -> memberInRange('room',$roomInfo -> all());
        $get['agent_id'] = $this -> agent_id;
        return view('admin.room.index',['data'=>$data,'roomInfo'=>$roomInfo,'agentInfo'=>$agentInfo,'request'=>$request->all(),'get'=>$get,'areaids' => $this->area_ids]);
    }

    //添加房间页面
    public function getCreate()
    {
        //获取代理商下的所有区域
        if($this -> area_ids === '0'){
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> get();
        }else{
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> whereIn("area.id",explode(",",$this -> area_ids)) -> get();
        }
        return view('admin/room/create',['areaInfo' => $areaInfo]);
    }

    //添加处理页面
    public function postStore(Request $request)
    {
        $info = $request -> except('_token');
        $info['agent_id'] = $this -> agent_id;
        //添加建筑表
        $res = Room::create($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    //编辑房间页面
    public function getEdit(Request $request)
    {
        $id = $request -> input('id');
        $data = Room::find($id) -> toarray();
        $data['area_id'] = Building::find($data['building_id']) -> area_id;
        $data['area_name'] = Area::find($data['area_id']) -> name;
        //获取代理商下的所有区域
        if($this -> area_ids === '0'){
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> get();
        }else{
            $areaInfo = DB::table('area') ->where('agent_id',$this->agent_id) -> whereIn("area.id",explode(",",$this -> area_ids)) -> get();
        }
        //获取该房间所在区域的所有建筑
        $buildingInfo = Building::where('area_id',$data['area_id']) -> get() -> toarray();
        return view('admin.room.edit',['data'=>$data,'areaInfo'=>$areaInfo,'buildingInfo'=>$buildingInfo]);
    }

    //编辑处理页面
    public function postUpdate(Request $request)
    {
        $id = $request -> input('id');
        $info = $request -> except('id','_token');
        $res = Room::find($id) -> update($info);
        if($res){
            $data['status'] = 1;
        }else{
            $data['status'] = -1;
        }
        echo json_encode($data);
    }

    //删除处理页面
    public function postDestroy(Request $request)
    {
        $id = $request -> input('id');
        //查询该房间下是否已绑定设备
        $num = DB::table('nb_bind') -> where('room_id',$id) -> count();
        $num += DB::table('smoke') -> where('room_id',$id) -> count();
        $num += DB::table('infrared') -> where('room_id',$id) -> count();
        $num += DB::table('electric') -> where('room_id',$id) -> count();
        $num += DB::table('help') -> where('room_id',$id) -> count();
        $num += DB::table('gas') -> where('room_id',$id) -> count();
        if($num){
            $data['status'] = -2;
        }else{
            //查询区域和客户的关系表并删除
            Roommember::where(['room_id'=>$id]) ->delete();
            $res = Room::destroy($id);
            if($res){
                $data['status'] = 1;
            }else{
                $data['status'] = -1;
            }
        }
        echo json_encode($data);
    }

    //追加负责人(客户)
    public function postAddmember(Request $request)
    {
        //获取post数据
        $room_id = $request -> input('id');
        $member = $request -> input('member');
        $info = $this -> addMember('room',$room_id,$member);
        echo json_encode($info);
    }

    //删除负责人(客户)
    public function postDelmember(Request $request)
    {
        //获取post数据
        $room_id = $request -> input('room_id');
        $member_id = $request -> input('member_id');
        //查询区域和客户的关系表并删除
        $info = $this -> delMember('room',$room_id,$member_id);
        echo json_encode($info);
    }

    //区域建筑联动
    public function postGetbuilding(Request $request){
        $area_id = $request -> input('area_id');
        $buildingInfo = Building::where('area_id',$area_id) -> get() -> toarray();
        return json_encode($buildingInfo);
    }
}
